package Service;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import Dao.WeiboStatus;
import Data.Status;

public class WeiboResponse implements Response{
    	
    	private StringBuffer jsonString ;
    	
    	private String userId ;
    	
    	private List<Status> existStatus ;
    	
    	private WeiboStatus weiboStatus ;
    	
    	public WeiboResponse( String userId ){
    	    this.userId = userId ;
    	    weiboStatus = new WeiboStatus() ;
    	    weiboStatus.deleteStatus(userId); ;
    	}
    	
	public StringBuffer getJsonString() {
	    return jsonString;
	}
	
	@Override
	public void setJsonString(StringBuffer jsonString) {
	    this.jsonString = jsonString;
	    
	    System.out.println(jsonString.toString());
	}
    	
	@Override
        public void parse(){
                      
            JSONObject jsonObject = JSONObject.fromObject(jsonString.toString()) ;
            
            JSONArray jsonArray = jsonObject.getJSONArray("statuses") ;

            for( int i = 0 ; i <jsonArray.size() ; i++){
        	weiboStatus.insertStatus(parse(jsonArray.getJSONObject(i))) ;
            }
            
        }
	
	
	private Status parse( JSONObject jsonObject ){
	    
	    Status statu = new Status() ;
	    
	    statu.setWeiboId(jsonObject.getString("idstr"));
	    statu.setText(jsonObject.getString("text"));
	    statu.setRetweeted_json(jsonObject.getJSONObject("retweeted_status").toString());
	    //statu.setPic_urls_json(jsonObject.getJSONObject("pic_urls").toString());
	    
	    JSONObject user = jsonObject.getJSONObject("user") ;
	    
	    statu.setSenderId(user.getString("idstr"));
	    statu.setSenderName(user.getString("name"));
	    
	    statu.setUserId(userId);
	    
	    return statu ;
	}
}
